package com.kingint.web.portal.timing;

import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.BasDepartment;
import com.kingint.mbg.ser.entity.SerCycleCleanPlan;
import com.kingint.mbg.ser.entity.SerSendMessage;
import com.kingint.web.admin.base.service.BasDepartmentService;
import com.kingint.web.portal.serve.service.SendMessageService;
import com.kingint.web.portal.serve.service.CycleCleanDetailsService;
import com.kingint.web.portal.serve.service.CycleCleanPlanService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author N_Xiang
 * @describe
 * @time 2021-01-12 下午 1:06
 */
@Component
public class regularlyPerformSerCycleCleanPlan {

    @Resource
    private CycleCleanDetailsService cycleCleanDetailsService;
    @Resource
    private CycleCleanPlanService cycleCleanPlanService;
    @Resource
    private BasDepartmentService basDepartmentService;
    @Resource
    private SendMessageService sendMessageService;

    private final int weeka[] = new int[]{7,1,2,3,4,5,6};

    //每天上午8点15开始分发周期任务
    @Scheduled(cron = "0 15 8 * * ?")
    public void regularPerform(){
        //获取所有计划
//        QueryWrapper<SerCycleCleanDetails> cleanDetailsQueryWrapper = new QueryWrapper<>();
//        cleanDetailsQueryWrapper.ge("end_time", DateUtils.format(new Date(),DateUtils.DATE_FORMAT_10));
//        List<SerCycleCleanDetails> list = serCycleCleanDetailsService.list(cleanDetailsQueryWrapper);
        List<SerCycleCleanPlan> getalllist = cycleCleanPlanService.getalllist();
        //过滤已过期计划
        List<SerCycleCleanPlan> collect = getalllist.stream().filter(v -> new Date().before(v.getEndTime())).collect(Collectors.toList());
        //获取
        Calendar cal = Calendar.getInstance();
        int day=cal.get(Calendar.DATE);//获取日
        int week = weeka[cal.get(Calendar.DAY_OF_WEEK)-1];//周几

        for (SerCycleCleanPlan s : collect) {
            switch (s.getType()){
                case 1: //天
                        send(s);
                    break;
                case 2: //周
                    if (s.getSerCycleCleanDetails().getWeek().contains("|"+week+"|")){
                        send(s);
                    }
                    break;
                case 3: //月
                    if (s.getSerCycleCleanDetails().getDayNum().contains("|"+day+"|")){
                        send(s);
                    }
                    break;
            }
        }



    }

    public void send(SerCycleCleanPlan plan){
        SerSendMessage serSendMessage = new SerSendMessage();
        String contant = "今天是"+DateUtils.format(new Date(),DateUtils.DATE_FORMAT_10)+"<p>今日的周期任务：<p>"+plan.getSerCycleCleanDetails().getTaskContent()+"<p>"
                +"请完成并回复。";
        serSendMessage.setContent(contant);
        QueryWrapper<BasDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hotel_id",plan.getHotelId());
        List<BasDepartment> list = basDepartmentService.list(queryWrapper);
        serSendMessage.setDepartmentList(list);
        serSendMessage.setSendTyp(0);
        serSendMessage.setIsReply(1);
        //使用群发通知发送给所有部门
        sendMessageService.insertSendMessageInfo(serSendMessage);
    }

}
